package org.framework.common.api.entity;

import java.io.File;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.framework.common.api.support.constant.SystemConstants;
import org.framework.common.api.support.model.FileModel;
import org.framework.common.ext.util.file.FileUtil;
import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * 文件
 * <p>描述: </p>
 * <p>Company: Professional</p>
 * @author chanlong(陈龙)
 * @date 2016年3月9日 下午8:58:41
 * @version 1.0.2016
 */
@Entity @Table(name = "TB_CORE_FILE")
public class CommonFile extends CommonEntityUUID {

	/** serialVersionUID(long):. */
	private static final long serialVersionUID = -4339006636085797035L;
	
	// 访问地址
	@Column(name = "url", length = 500, nullable = true)
	private String url;
	
	// 存储类型（file、data）
	@Column(name = "mode", length = 10, nullable = true, columnDefinition="varchar(10) default 'file'")
	private String mode;
		
	// 文件类型（file、image、video、audio）
	@Column(name = "type", length = 10, nullable = true, columnDefinition="varchar(10) default 'file'")
	private String type;
	
	// 文件名称
	@Column(name = "name", length = 100, nullable = true)
	private String name;
	
	// 文件路径
	@Column(name = "path", length = 500, nullable = true)
	private String path;
	
	// 文件大小
	@Column(name = "length", nullable = true)
	private Long length;
	
	// 文件版本
	@Column(name = "version", length = 500, nullable = true)
	private String version;
	
	// 文件内容
	@JsonIgnore
	@Lob @Column(name = "content", nullable = true)
	private byte[] content;
	
	// 文件后缀
	@Column(name = "extension", length = 10, nullable = true)
	private String extension;
		
	// 创建时间
	@Temporal(TemporalType.DATE)
	@JsonFormat(pattern="yyyy-MM-dd", timezone="GMT+8")
	@DateTimeFormat(pattern="yyyy-MM-dd")
	@Column(name = "create_date", nullable = true)
	private Date createDate;

	// 记录ID
	@Column(name = "record_id", length = 50, nullable = true)
	private String recordId;
	
	public CommonFile() {
		// TODO Auto-generated constructor stub
	}
	
	public CommonFile(final FileModel model, final File file) throws Exception {
		String filemode = model.getMode();
		String filetype = model.getType();
		String fullname = file.getAbsolutePath();
		String filename = FilenameUtils.getBaseName(fullname);
		String extension = FilenameUtils.getExtension(fullname);
		
		this.setMode(filemode);
		this.setType(filetype);
		this.setName(filename);
		this.setPath(fullname);
		this.setLength(file.length());
		this.setStatus(SystemConstants.DICT_STATUS_DEFAULT);
		this.setInvalid(SystemConstants.DICT_DELETE_INVALID);
		this.setRecordId(model.getRecordId());
		this.setExtension(extension);
		this.setCreateDate(new Date());
		
		if(filemode.equals(FileUtil.UPLOAD_MODE_DATA)){
			this.setContent(FileUtils.readFileToByteArray(file));
			FileUtils.deleteQuietly(file);
		}else if(filemode.equals(FileUtil.UPLOAD_MODE_FILE)){
			String fileurl = new StringBuilder(FileUtil.UPLOAD_URL).append(fullname.replace(FileUtil.UPLOAD_PATH, "")).toString();
			fileurl = fileurl.replace(File.separator,"/");
			this.setUrl(fileurl);
		}
	}
	
	/**
	 * 删除文件
	 * .
	 * @param path
	 * @author Administrator(zhaochong)
	 * @date 2016年5月5日 上午11:24:50
	 */
	public void delete(){
		File file = new File(path);
		if(file.exists()){
			file.delete();
		}
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getMode() {
		return mode;
	}

	public void setMode(String mode) {
		this.mode = mode;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public Long getLength() {
		return length;
	}

	public void setLength(Long length) {
		this.length = length;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public byte[] getContent() {
		return content;
	}

	public void setContent(byte[] content) {
		this.content = content;
	}

	public String getExtension() {
		return extension;
	}

	public void setExtension(String extension) {
		this.extension = extension;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public String getRecordId() {
		return recordId;
	}

	public void setRecordId(String recordId) {
		this.recordId = recordId;
	}
}
